package testy;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

public class DynamicJTreeDemo extends JFrame{

	private static int newNodeCounter;

	private static String ADD_ACTION = "add";

	private static String REMOVE_ACTION = "remove";

	private static String CLEAR_ACTION = "clear";

	private DefaultMutableTreeNode rootNode;

	private DefaultTreeModel defaultTreeModel;

	private JTree jtree;

	public DynamicJTreeDemo() {

		//    	Creates the JTree
		rootNode = new DefaultMutableTreeNode("Root Node");
		defaultTreeModel = new DefaultTreeModel(rootNode);

		jtree = new JTree(defaultTreeModel);
		jtree.setEditable(true);
		jtree.getSelectionModel().setSelectionMode(
				TreeSelectionModel.SINGLE_TREE_SELECTION);
		jtree.setShowsRootHandles(true);
		initializeTree();
		JScrollPane jscrollPane = new JScrollPane(jtree);
		JPanel treePanel = new JPanel();
		treePanel.add(jscrollPane);

		JPanel buttonPanel = new JPanel();

		JButton jbnAdd = new JButton("Add");
		jbnAdd.setActionCommand(ADD_ACTION);
//		jbnAdd.addActionListener(this);
		buttonPanel.add(jbnAdd, BorderLayout.WEST);

		JButton jbnRemove = new JButton("Remove");
		jbnRemove.setActionCommand(REMOVE_ACTION);
//		jbnRemove.addActionListener(this);
		buttonPanel.add(jbnRemove, BorderLayout.SOUTH);

		JButton jbnClear = new JButton("Clear");
		jbnClear.setActionCommand(CLEAR_ACTION);
//		jbnClear.addActionListener(this);
		buttonPanel.add(jbnClear, BorderLayout.EAST);

		JSplitPane jsplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
				treePanel, buttonPanel);
		//Lay everything out.
		//        setPreferredSize(new Dimension(300, 150));
		getContentPane().add(jsplitPane);

	}

	private static void createAndShowGUI() {

		DynamicJTreeDemo dynamicJTreeDemo = new DynamicJTreeDemo();
		dynamicJTreeDemo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		dynamicJTreeDemo.setTitle("Dynamic JTree Demo");

		dynamicJTreeDemo.pack();
		dynamicJTreeDemo.setVisible(true);
	}

	public void initializeTree() {
		String p1 = new String("Parent 1"); //p1 = Parent 1
		String p2 = new String("Parent 2");
		String c1 = new String("Child 1");
		String c2 = new String("Child 2");

		DefaultMutableTreeNode p1Node = null, p2Node = null;

		p1Node = addObject(null, p1, false);
		p2Node = addObject(null, p2, false);

		addObject(p1Node, c1, false);
		addObject(p1Node, c2, false);

		addObject(p2Node, c1, false);
		addObject(p2Node, c2, false);

	}

	public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
			Object child, boolean isVisible) {

		DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child);

		if (parent == null) {
			parent = rootNode;
		}

		defaultTreeModel.insertNodeInto(childNode, parent, parent
				.getChildCount());

		if (isVisible) {
			jtree.scrollPathToVisible(new TreePath(childNode.getPath()));
		}

		return childNode;
	}

	public static void main(String[] args) {

		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		});
	}

}